using UnityEngine;
using System.Collections;

public class HumanScript : PlayerScript {
	GunScript my_gun;
	ParticleScript myparticle;
	public int shot_charge = 0;
	public AudioClip humanHurt;
	public AudioClip humanDie;
	private BulletBar GunBarScript;
	// Use this for initialization
	void Start () {
		myparticle = gameObject.GetComponent<ParticleScript>();
		my_gun = gameObject.GetComponentInChildren<GunScript>();
		GunBarScript = UIBar.FindChild("BulletBar").GetComponent<BulletBar>();
		GunBarScript.SetCurrent(my_gun.ammunition);
		shot_charge = 0;
	}
	
	void OnSpawned()
	{
				
	}
	
	// Update is called once per frame
	void Update () {
		Vector2 twinstik_shot;
		if (Health <=0 && base.isAlive) 
		{ 
			audio.Stop();
			audio.clip = humanDie;
			audio.Play();
			playerDies(); 
		}
		
		twinstik_shot = new Vector2(Input.GetAxis("R_XAxis_1"), Input.GetAxis("R_YAxis_1"));
		if (shot_charge > 0) shot_charge -= 1;
		if (twinstik_shot.magnitude > 0.25) {
			Debug.Log("Maishot");
			Debug.Log(twinstik_shot.x);	
			Debug.Log(twinstik_shot.y);	
			shot_charge += 2;
			if (shot_charge > 30 && my_gun.ammunition > 0) {
				my_gun.OnFire(twinstik_shot.normalized);
				shot_charge = 0;
			}
		}
	}
	
	/*void OnTriggerStay(Collider col)
	{
		switch(col.gameObject.tag)
		{
		case "alien":
			AlienScript alienScript = col.gameObject.GetComponent<AlienScript>();
			if(alienScript.attacking == true && Health > 0)
			{
				alienScript.attackTimer += Time.deltaTime;
				if (alienScript.attackTimer >= alienScript.attackDuration) {
					Health -= alienScript.attackDamage;
					alienScript.attackTimer = 0.0f;
					alienScript.attacking = false;
					alienScript.FinishAttack();
					audio.Stop();
					audio.clip = humanHurt;
					audio.Play();
					//HBar.SetBarValue(myHealth);
					
				}
			} 
			else if (Health > 0 && alienScript.Health > 0) 
			{ 
				//alienScript.StartAttack(); 
				//alienScript.attackTimer += Time.deltaTime; 
				//HBar.SetBarValue(myHealth);
			}
			break;
		}
	}*/
	
	void OnControllerColliderHit(ControllerColliderHit col) 
	{
		if (col.gameObject.tag != "Untagged")
		{
		Debug.Log(col.gameObject.name);
				PoolManager.Pools["Powerups"].Despawn(col.transform);
			
		switch (col.gameObject.tag) 
			{
			case "health":
				ItemScript healthScript = col.gameObject.GetComponent<ItemScript>();
				int healthValue = healthScript.healthValue;
				Health += healthValue;
				//Destroy(healthScript.gameObject);
			break;
			case "ammo":
				myparticle.PlayAmmoEffect();
				//Debug.Log("madeittoplay"); 
				ItemScript ammoScript = col.gameObject.GetComponent<ItemScript>();
				my_gun.ammunition += 1;
				GunBarScript.SetCurrent(my_gun.ammunition);
				//Destroy(ammoScript.gameObject);
			break;
			case "speed":
				myparticle.PlaySpeedEffect();
				//Destroy(col.gameObject);
			break;
		}
		}
	}
}
